package com.imonsoft.sqm.service.business.supplier_quality;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.pojo.business.supplier_quality.AnswerProblem;
import com.imonsoft.sqm.pojo.business.supplier_quality.Audit;
import com.imonsoft.sqm.pojo.business.supplier_quality.EnvManage;
import com.imonsoft.sqm.pojo.business.supplier_quality.QualitySystem;
import com.imonsoft.sqm.pojo.business.supplier_quality.Special_Claim;
import com.imonsoft.sqm.service.basedata.IpicTargetService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.service.business.ipic_system.IPICService;
import com.imonsoft.sqm.vo.business.supplier_quality.SupplierDataVo;

@Service("supplierDataService")
public class SupplierDataServiceImpl implements SupplierDataService {

	@Autowired
	private SupplierService supplierService;

	@Autowired
	private QualitySystemService qualitySystemService;

	@Autowired
	private AuditService auditService;

	@Autowired
	private AnswerProblemService answerProblemService;

	@Autowired
	private SpecialClaimService specialClaimService;

	@Autowired
	private EnvManageService envManageService;
	
	@Autowired
	private IpicTargetService ipicTargetService;
	
	@Autowired
	private IPICService iPICService;

	@Override
	public DataGrid<SupplierDataVo> getDataGridVoBy(SupplierDataVo vo, Pager pager) {
		Integer count = null;
		List<SupplierDataVo> rows = new ArrayList<SupplierDataVo>();
		List<Supplier> lists = supplierService.getAllSupplier(pager);
		List<Supplier> list=supplierService.findList(null);
		if (list != null && list.size() != 0) {
			count = list.size();

			for (Supplier supplier : lists) {
				SupplierDataVo bean = new SupplierDataVo();
				BigDecimal total = new BigDecimal(0);
				// 质量体系
				QualitySystem entity = new QualitySystem();
				entity.setSupplierId(Integer.parseInt(StringUtil.fixup(supplier.getId())));
				List<QualitySystem> quality = qualitySystemService.findList(entity);
				if (quality != null && quality.size() != 0) {
					bean.setQmSystem(StringUtil.fixup(quality.get(0).getValue()));
					if (quality.get(0).getValue() != null){
						total = total.add(new BigDecimal(quality.get(0).getValue()));
					}else{
						bean.setQmSystem("0");
					}
				}else{
					bean.setQmSystem("0");
				}

				// 过程审核得分
				Audit audit = new Audit();
				audit.setSupplierId(Integer.parseInt(StringUtil.fixup(supplier.getId())));
				List<Audit> auditList = auditService.selectAuditByAudit(audit);
				if (auditList != null && auditList.size() != 0) {
					bean.setProcessAudits(StringUtil.fixup(auditList.get(0).getValue()));
					if (auditList.get(0).getValue() != null)
						total = total.add(new BigDecimal(auditList.get(0).getValue()));
					else
						bean.setProcessAudits("0");	
				}else{
					bean.setProcessAudits("0");
				}
				
				//0公里交付质量得分 
				Map<String, Object> searchMap = new HashMap<String, Object>();
				searchMap.put("supplierId", Long.valueOf(supplier.getId()));
				searchMap.put("year", vo.getYear());
			    searchMap.put("type", ConstantSqm.TYPE_CLAIM);
				searchMap.put("month", StringUtil.paddingZero(vo.getMonth(), 2)); 
				Map<String, Object> resultMap = new HashMap<String, Object>();
				iPICService.calIPICMonthData(searchMap, resultMap);
				
				Integer value = (Integer) resultMap.get("ipicMonth");
				Double target = ipicTargetService.getIpicTargetValue(searchMap); 
				if(target!=null&&value!=null){
					if(value<=target.doubleValue()){
						bean.setMilesDeliveryQuality("35");
						total = total.add(new BigDecimal("35"));
					}else  if(target.doubleValue()<value&&value<=target.doubleValue()*1.25){
						bean.setMilesDeliveryQuality("30");
						total = total.add(new BigDecimal("30"));
					}else if(target.doubleValue()*1.25<value&&value<=target.doubleValue()*1.5){
						bean.setMilesDeliveryQuality("20");
						total = total.add(new BigDecimal("20"));
					}else if(target.doubleValue()*1.5<value&&value<=target.doubleValue()*1.75){
						bean.setMilesDeliveryQuality("10");
						total = total.add(new BigDecimal("10"));
					}else{
						bean.setMilesDeliveryQuality("0");
					} 
					
				}else{
					bean.setMilesDeliveryQuality("0");
				}
				

				// 问题反映进度得分
				AnswerProblem answerProblem = new AnswerProblem();
				answerProblem.setSupplierId(Integer.parseInt(StringUtil.fixup(supplier.getId())));
				answerProblem.setYear(vo.getYear());
				answerProblem.setMonth(StringUtil.paddingZero(vo.getMonth(), 2));
				List<AnswerProblem> answerProblemList = answerProblemService.findListByState(answerProblem);
				if (answerProblemList != null && answerProblemList.size() != 0) {
					bean.setResponseOnProblems(StringUtil.fixup(answerProblemList.get(0).getValue()));
					if (answerProblemList.get(0).getValue() != null)
						total = total.add(new BigDecimal(answerProblemList.get(0).getValue()));
					else
						bean.setResponseOnProblems("0");	
				}else{
					bean.setResponseOnProblems("0");
				}

				// 特殊投诉
				Special_Claim special_Claim = new Special_Claim();
				special_Claim.setSupplierId(Integer.parseInt(StringUtil.fixup(supplier.getId())));
				special_Claim.setYear(vo.getYear());
				special_Claim.setMonth(StringUtil.paddingZero(vo.getMonth(), 2));
				List<Special_Claim> specialClaimList = specialClaimService.findListByStateMonth(special_Claim);
				if (specialClaimList != null && specialClaimList.size() != 0) {
					bean.setDeductiblesForSpecialIncidents(StringUtil.fixup(specialClaimList.get(0).getValue()));
					if (specialClaimList.get(0).getValue() != null)
						total = total.add(new BigDecimal(specialClaimList.get(0).getValue()));
					else
						bean.setDeductiblesForSpecialIncidents("0");	
				}else{
					bean.setDeductiblesForSpecialIncidents("0");
				}

				// 环境体系得分
				EnvManage envManage = new EnvManage();
				envManage.setSupplierId(Integer.parseInt(StringUtil.fixup(supplier.getId())));
				List<EnvManage> envManageList = envManageService.findList(envManage);
				if (envManageList != null && envManageList.size() != 0) {
					bean.setEnvironmentalManagementSystem(StringUtil.fixup(envManageList.get(0).getValue()));
					if (envManageList.get(0).getValue() != null)
						total = total.add(new BigDecimal(envManageList.get(0).getValue()));
					else
						bean.setEnvironmentalManagementSystem("0");
				}else{
					bean.setEnvironmentalManagementSystem("0");
				}

				// 总得分
				bean.setEvaluationValue(StringUtil.fixup(total.longValue()));

				// 供应商等级
				if (Long.valueOf(bean.getEvaluationValue()) < 80) {
					bean.setSupplierRating("C");
				} else if (Long.valueOf(bean.getEvaluationValue()) < 90) {
					bean.setSupplierRating("B");
				} else  {
					bean.setSupplierRating("A");
				} 

				bean.setSupplierId(StringUtil.fixup(supplier.getId()));
				bean.setSupplierCode(supplier.getCode());
				bean.setSupplierName(supplier.getName());
				bean.setYear(vo.getYear());
				bean.setMonth(vo.getMonth());
				rows.add(bean);
			}
		} else {
			count = 0;
		}
		DataGrid<SupplierDataVo> dataGrid = new DataGrid<SupplierDataVo>(count, rows);
		return dataGrid;
	}

	@Override
	public DataGrid<SupplierDataVo> getDataGridVoByTwo(SupplierDataVo vo, Pager pager) {
		List<SupplierDataVo> rows = new ArrayList<SupplierDataVo>();
		Integer count = 12;

		Supplier supplier = supplierService.get(Long.valueOf(vo.getSupplierId()));
		vo.setSupplierName(supplier.getName());
		vo.setSupplierCode(supplier.getCode());
		for (int i = 1; i <= count; i++) {
			SupplierDataVo bean = new SupplierDataVo();
			BigDecimal total = new BigDecimal(0);
			// 质量体系
			QualitySystem entity = new QualitySystem();
			entity.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
			List<QualitySystem> quality = qualitySystemService.findList(entity);
			if (quality != null && quality.size() != 0) {
				bean.setQmSystem(StringUtil.fixup(quality.get(0).getValue()));
				if (quality.get(0).getValue() != null)
					total = total.add(new BigDecimal(quality.get(0).getValue()));
				else
					bean.setQmSystem("0");	
			}else{
				bean.setQmSystem("0");
			}

			// 过程审核得分
			Audit audit = new Audit();
			audit.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
			List<Audit> auditList = auditService.selectAuditByAudit(audit);
			if (auditList != null && auditList.size() != 0) {
				bean.setProcessAudits(StringUtil.fixup(auditList.get(0).getValue()));
				if (auditList.get(0).getValue() != null)
					total = total.add(new BigDecimal(auditList.get(0).getValue()));
				else
					bean.setProcessAudits("0");	
			}else{
				bean.setProcessAudits("0");
			}

			// 问题反映进度得分
			AnswerProblem answerProblem = new AnswerProblem();
			answerProblem.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
			answerProblem.setYear(vo.getYear());
			answerProblem.setMonth(StringUtil.paddingZero(String.valueOf(i),2));
			List<AnswerProblem> answerProblemList = answerProblemService.findListByState(answerProblem);
			if (answerProblemList != null && answerProblemList.size() != 0) {
				bean.setResponseOnProblems(StringUtil.fixup(answerProblemList.get(0).getValue()));
				if (answerProblemList.get(0).getValue() != null)
					total = total.add(new BigDecimal(answerProblemList.get(0).getValue()));
				else
					bean.setResponseOnProblems("0");	
			}else{
				bean.setResponseOnProblems("0");
			}
			
			//0公里交付质量得分 
			Map<String, Object> searchMap = new HashMap<String, Object>();
			searchMap.put("supplierId", Long.valueOf(supplier.getId()));
			searchMap.put("year", vo.getYear());
		   searchMap.put("type", ConstantSqm.TYPE_CLAIM);
			searchMap.put("month", StringUtil.paddingZero(String.valueOf(i), 2)); 
			Map<String, Object> resultMap = new HashMap<String, Object>();
			iPICService.calIPICMonthData(searchMap, resultMap);
			
			Integer value = (Integer) resultMap.get("ipicMonth");
			Double target = ipicTargetService.getIpicTargetValue(searchMap); 
			if(target!=null&&value!=null){
				if(value<=target.doubleValue()){
					bean.setMilesDeliveryQuality("35");
					total = total.add(new BigDecimal("35"));
				}else  if(target.doubleValue()<value&&value<=target.doubleValue()*1.25){
					bean.setMilesDeliveryQuality("30");
					total = total.add(new BigDecimal("30"));
				}else if(target.doubleValue()*1.25<value&&value<=target.doubleValue()*1.5){
					bean.setMilesDeliveryQuality("20");
					total = total.add(new BigDecimal("20"));
				}else if(target.doubleValue()*1.5<value&&value<=target.doubleValue()*1.75){
					bean.setMilesDeliveryQuality("10");
					total = total.add(new BigDecimal("10"));
				}else{
					bean.setMilesDeliveryQuality("0");
				} 
			
			}else{
				bean.setMilesDeliveryQuality("0");
			}
			
			// 特殊投诉
			Special_Claim special_Claim = new Special_Claim();
			special_Claim.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
			special_Claim.setYear(vo.getYear());
			special_Claim.setMonth(StringUtil.paddingZero(String.valueOf(i),2));
			List<Special_Claim> specialClaimList = specialClaimService.findListByStateMonth(special_Claim);
			if (specialClaimList != null && specialClaimList.size() != 0) {
				bean.setDeductiblesForSpecialIncidents(StringUtil.fixup(specialClaimList.get(0).getValue()));
				if (specialClaimList.get(0).getValue() != null)
					total = total.add(new BigDecimal(specialClaimList.get(0).getValue()));
				else
					bean.setDeductiblesForSpecialIncidents("0");	
			}else{
				bean.setDeductiblesForSpecialIncidents("0");
			}

			// 环境体系得分
			EnvManage envManage = new EnvManage();
			envManage.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
			List<EnvManage> envManageList = envManageService.findList(envManage);
			if (envManageList != null && envManageList.size() != 0) {
				bean.setEnvironmentalManagementSystem(StringUtil.fixup(envManageList.get(0).getValue()));
				if (envManageList.get(0).getValue() != null)
					total = total.add(new BigDecimal(envManageList.get(0).getValue()));
				else
					bean.setEnvironmentalManagementSystem("0");	
			}else{
				bean.setEnvironmentalManagementSystem("0");
			}

			// 总得分
			bean.setEvaluationValue(StringUtil.fixup(total.longValue()));

			// 供应商等级
			if (Long.valueOf(bean.getEvaluationValue()) < 80) {
				bean.setSupplierRating("C");
			} else if (Long.valueOf(bean.getEvaluationValue()) < 90) {
				bean.setSupplierRating("B");
			} else  {
				bean.setSupplierRating("A");
			} 

			bean.setSupplierId(StringUtil.fixup(vo.getSupplierId()));
			bean.setSupplierCode(vo.getSupplierCode());
			bean.setSupplierName(vo.getSupplierName());
			bean.setYear(vo.getYear());
			bean.setMonth(StringUtil.paddingZero(String.valueOf(i), 2));
			rows.add(bean);

		}
		DataGrid<SupplierDataVo> dataGrid = new DataGrid<SupplierDataVo>(count, rows);
		return dataGrid;
	}

	@Override
	public SupplierDataVo getDataGridVoByList(SupplierDataVo vo) {

		Supplier supplier = supplierService.get(Long.valueOf(vo.getSupplierId()));
		vo.setSupplierName(supplier.getName());
		vo.setSupplierCode(supplier.getCode());
		vo.setSupplierType(supplier.getMaterialGroup());
		
		BigDecimal total = new BigDecimal(0);
		// 质量体系
		QualitySystem entity = new QualitySystem();
		entity.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
		List<QualitySystem> quality = qualitySystemService.findList(entity);
		if (quality != null && quality.size() != 0) {
			vo.setQmSystem(StringUtil.fixup(quality.get(0).getValue()));
			if (quality.get(0).getValue() != null)
				total = total.add(new BigDecimal(quality.get(0).getValue()));
		}

		// 过程审核得分
		Audit audit = new Audit();
		audit.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
		List<Audit> auditList = auditService.selectAuditByAudit(audit);
		if (auditList != null && auditList.size() != 0) {
			vo.setProcessAudits(StringUtil.fixup(auditList.get(0).getValue()));
			if (auditList.get(0).getValue() != null)
				total = total.add(new BigDecimal(auditList.get(0).getValue()));
		}

		// 问题反映进度得分
		AnswerProblem answerProblem = new AnswerProblem();
		answerProblem.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
		answerProblem.setYear(vo.getYear());
		answerProblem.setMonth(vo.getMonth());
		List<AnswerProblem> answerProblemList = answerProblemService.findListByState(answerProblem);
		if (answerProblemList != null && answerProblemList.size() != 0) {
			vo.setResponseOnProblems(StringUtil.fixup(answerProblemList.get(0).getValue()));
			if (answerProblemList.get(0).getValue() != null)
				total = total.add(new BigDecimal(answerProblemList.get(0).getValue()));
		}
		
		//0公里交付质量得分 
		Map<String, Object> searchMap = new HashMap<String, Object>();
		searchMap.put("supplierId", Long.valueOf(supplier.getId()));
		searchMap.put("year", vo.getYear());
		searchMap.put("type", ConstantSqm.TYPE_CLAIM);
		searchMap.put("month", StringUtil.paddingZero(vo.getMonth(), 2)); 
		Map<String, Object> resultMap = new HashMap<String, Object>();
		iPICService.calIPICMonthData(searchMap, resultMap);
		
		Integer value = (Integer) resultMap.get("ipicMonth");
		Double target = ipicTargetService.getIpicTargetValue(searchMap); 
		if(target!=null&&value!=null){
			
			if(value<=target.doubleValue()){
				vo.setMilesDeliveryQuality("35");
				total = total.add(new BigDecimal("35"));
			}else  if(target.doubleValue()<value&&value<=target.doubleValue()*1.25){
				vo.setMilesDeliveryQuality("30");
				total = total.add(new BigDecimal("30"));
			}else if(target.doubleValue()*1.25<value&&value<=target.doubleValue()*1.5){
				vo.setMilesDeliveryQuality("20");
				total = total.add(new BigDecimal("20"));
			}else if(target.doubleValue()*1.5<value&&value<=target.doubleValue()*1.75){
				vo.setMilesDeliveryQuality("10");
				total = total.add(new BigDecimal("10"));
			}else{
				vo.setMilesDeliveryQuality("0");
			} 
			
		
		}else{
			vo.setMilesDeliveryQuality("0");
		}

		// 特殊投诉
		Special_Claim special_Claim = new Special_Claim();
		special_Claim.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
		special_Claim.setYear(vo.getYear());
		special_Claim.setMonth(StringUtil.paddingZero(vo.getMonth(), 2));
		List<Special_Claim> specialClaimList = specialClaimService.findListByStateMonth(special_Claim);
		if (specialClaimList != null && specialClaimList.size() != 0) {
			vo.setDeductiblesForSpecialIncidents(StringUtil.fixup(specialClaimList.get(0).getValue()));
			if (specialClaimList.get(0).getValue() != null)
				total = total.add(new BigDecimal(specialClaimList.get(0).getValue()));
		}

		// 环境体系得分
		EnvManage envManage = new EnvManage();
		envManage.setSupplierId(Integer.parseInt(StringUtil.fixup(vo.getSupplierId())));
		List<EnvManage> envManageList = envManageService.findList(envManage);
		if (envManageList != null && envManageList.size() != 0) {
			vo.setEnvironmentalManagementSystem(StringUtil.fixup(envManageList.get(0).getValue()));
			if (envManageList.get(0).getValue() != null)
				total = total.add(new BigDecimal(envManageList.get(0).getValue()));
		}

		// 总得分
		vo.setEvaluationValue(StringUtil.fixup(total.longValue()));

		// 供应商等级
		if (Long.valueOf(vo.getEvaluationValue()) < 80) {
			vo.setSupplierRating("C");
		} else if (Long.valueOf(vo.getEvaluationValue()) < 90) {
			vo.setSupplierRating("B");
		} else  {
			vo.setSupplierRating("A");
		} 
		return vo;
	}

}
